package com.spark.incoicings.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.spark.incoicings.dao.CustomerFollowUpMapper;
import com.spark.invoicings.entity.CustomerFollowUp;
import com.spark.invoicings.utils.Result;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/login")
public class CustomerFollowUpController {

    @Resource
    private CustomerFollowUpMapper followUpMapper;

    @GetMapping("/selectFollowUpAll")
    public Result selectFollowUpAll(@RequestParam(defaultValue = "1")Integer pageNum,
                                    @RequestParam(defaultValue = "6")Integer pageSize,
                                    @RequestParam(value = "customerName",required = false)String customerName,
                                    @RequestParam(value = "time",required = false)String time){
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<CustomerFollowUp> wrapper = new QueryWrapper<>();
        if (customerName!=null){
            wrapper.like("customer_name",customerName);
        }
        if (time!=null){
            wrapper.eq("follow_up_date",time);
        }
        wrapper.orderByDesc("follow_up_date");
        List<CustomerFollowUp> followUps = followUpMapper.selectList(wrapper);
        PageInfo<CustomerFollowUp> follow = new PageInfo<>(followUps);
        return Result.success(200,"success",follow);
    }
    @GetMapping("/selectFollowUp")
    public Result selectFollowUp(Integer id){
        CustomerFollowUp followUp = followUpMapper.selectById(id);
        return Result.success(200,"success",followUp);
    }
    @PostMapping("/updateFollowUp")
    public Result updateFollowUp(@RequestBody CustomerFollowUp followUp){
        followUpMapper.updateById(followUp);
        return Result.success(200,"success",followUp);
    }
    @PostMapping("/deleteFollowUp")
    public Result deleteFollowUp(Integer id){
        followUpMapper.deleteById(id);
        return Result.success();
    }
    @PostMapping("/saveFollowUp")
    public Result saveFollowUp(@RequestBody CustomerFollowUp followUp){
        followUpMapper.insert(followUp);
        return Result.success();
    }
}
